<?php
declare (strict_types=1);

namespace app\services\adv;

use think\facade\Db;
use app\services\BaseServices;
use app\dao\adv\AdvListDao;
use crmeb\exceptions\AdminException;
use think\db\exception\{DataNotFoundException, DbException, ModelNotFoundException};

/**
 *
 * Class AdvListServices
 * @package app\services\adv
 * @mixin AdvListDao
 */
class AdvListServices extends BaseServices
{

    /**
     * AdvListServices constructor.
     * @param AdvListDao $dao
     */
    public function __construct(AdvListDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param array $whereData
     * @param array $with
     * @return array
     * @throws DbException
     */
    public function getAdvList(array $whereData, array $with = ['adv']): array
    {
        [$page, $limit] = $this->getPageValue();
        return $this->dao->getAdvList($whereData, $page, $limit, $with);
    }

    /**
     * @param array $data
     * @return bool
     */
    public function setAdvList(array $data): bool
    {
        if ($data['id'] == '') {
            $this->dao->save($data);
        } else {
            $this->dao->update($data['id'], $data);
        }
        return true;
    }

    /**
     * @param int $id
     * @return bool
     */
    public function del(int $id): bool
    {
        $this->dao->delete($id);
        $prefix = config('database.connections.mysql.prefix');
        Db::query('ALTER TABLE ' . $prefix . 'adv_list AUTO_INCREMENT =1');
        return true;
    }
}
